package com.gnerv.cms.controller;


import com.gnerv.cms.dto.ArticleDTO;
import com.gnerv.cms.entity.Article;
import com.gnerv.cms.entity.Content;
import com.gnerv.cms.entity.R;
import com.gnerv.cms.service.IArticleService;
import com.gnerv.cms.service.ICatalogService;
import com.gnerv.cms.service.IContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * CMS平台-文章表 前端控制器
 * </p>
 *
 * @author Gnerv LiGen
 * @since 2019-07-01
 */
@RestController
@RequestMapping("/cms/article")
public class ArticleController {
    @Autowired
    ICatalogService catalogService;

    @Autowired
    IArticleService articleService;

    @Autowired
    IContentService contentService;

    @RequestMapping(value = "create", method = RequestMethod.POST)
    public R createArticle(ArticleDTO article) {
        articleService.createArticle(article.getcId(), article.getArticle(), article.getContent());
        return R.back();
    }

    @RequestMapping(value = "delete/{bId}", method = RequestMethod.GET)
    public R deleteArticle(@PathVariable("bId") String bId) {
        articleService.deleteArticle(bId);
        return R.back();
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    public R updateArticle(ArticleDTO article) {
        articleService.updateArticle(article.getcId(), article.getArticle(), article.getContent());
        return R.back();
    }

    @RequestMapping(value = "select/{bId}", method = RequestMethod.GET)
    public R selectArticle(@PathVariable("bId") String bId) {
        ArticleDTO articleDTO = new ArticleDTO();
        Article article = articleService.selectArticle(bId);
        Content content = contentService.selectContentByAid(bId);
        articleDTO.setArticle(article);
        articleDTO.setContent(content);
        return R.back().data("article", articleDTO);
    }

    @RequestMapping(value = "list", method = RequestMethod.POST)
    public R list(@RequestBody Article article) {
        List<Article> articles = articleService.listArticle(article);
        return R.back().data("articles", articles);
    }
}
